﻿using FastJSON;
using FineAdmin.Common;
using HuiJI.Tools.TBK.JTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace HuiJI.Tools.TBK
{
    public static class TBKH5Api
    {
        public static WirelessDetailJTO.Rootobject GetWirelessDetail(string ItemId)
        {
            string url = "http://hws.m.taobao.com/d/modulet/v5/WItemMouldDesc.do?id=" + ItemId + "&f=TB1t8yefcbpK1RjSZFy8qt_qFla";
            var ret=HttpMethods.HttpGet(url);
            WirelessDetailJTO.Rootobject retObj = JSON.ToObject<WirelessDetailJTO.Rootobject>(ret);

            return retObj;
        }

        public static string[] GetItemDesc(string ItemId)
        {
            string url = "https://mdetail.tmall.com/templates/pages/desc?id=" + ItemId ;
            var ret = HttpMethods.HttpGet(url);
            return GetHtmlImageUrlList(ret);
        }
        /// <summary> 
        /// 取得HTML中所有图片的 URL。 
        /// </summary> 
        /// <param name="sHtmlText">HTML代码</param> 
        /// <returns>图片的URL列表</returns> 
        public static string[] GetHtmlImageUrlList(string sHtmlText)
        {
            // 定义正则表达式用来匹配 img 标签 
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            // 搜索匹配的字符串 
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            string[] sUrlList = new string[matches.Count];
            // 取得匹配项列表 
            foreach (Match match in matches)
                sUrlList[i++] = match.Groups["imgUrl"].Value;
            return sUrlList;
        }
    }
}
